package com.qj.platform.pay.core.dao;

import com.qj.base.commons.domain.SearchObject;
import com.qj.base.commons.utils.CodeGenerator;
import com.qj.base.commons.utils.DateUtils;
import com.qj.base.commons.utils.SqlUtil;
import com.qj.platform.pay.core.domain.Order;
import com.qj.platform.pay.mssm.domain.MSNotify;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhijie on 2016/12/15.
 */
@Repository
public class OrderDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private Dao nutzDao;

    public Order getById(String id){
        return nutzDao.fetch(Order.class,id);
    }

    public List<Order> search(SearchObject searchObject){
        List<Object> argList=new ArrayList<>();
        return jdbcTemplate.query(SqlUtil.getSearchSql(argList,"order_",searchObject),argList.toArray(new Object[]{}),new OrderRowMapper());
    }
    public int searchCount(SearchObject searchObject){
        List<Object> argList=new ArrayList<>();
        return jdbcTemplate.queryForObject(SqlUtil.getCountSql(argList,"order_",searchObject),argList.toArray(new Object[]{}),Integer.class);
    }

    public void update(Order order){
        order.setModifyTime(DateUtils.getCurrentDate());
        nutzDao.update(order);
    }

    public String insert(Order order){
        order.setOriReqMsgId(CodeGenerator.getUUID32());
        nutzDao.insert(order);
        return order.getOriReqMsgId();
    }

    class OrderRowMapper implements RowMapper<Order> {
        @Override
        public Order mapRow(ResultSet resultSet, int i) throws SQLException {
            Order order=new Order();
            order.setOriReqMsgId(resultSet.getString("id"));
            order.setTotalAmount(resultSet.getDouble("total_amount"));
            order.setCreateTime(resultSet.getTimestamp("create_time"));
            order.setModifyTime(resultSet.getTimestamp("modify_time"));
            order.setSettleDate(resultSet.getTimestamp("settle_date"));
            order.setSubject(resultSet.getString("subject"));
            order.setMerchantCode(resultSet.getString("merchant_code"));
            order.setStatus(resultSet.getString("status"));
            return order;
        }
    }

    public List<Order> queryByNutz(){
        Sql sql = Sqls.create("SELECT o.*,m.merchant_name FROM order_ o JOIN merchant m ON m.merchant_code=o.merchant_code");
            sql.setCallback(Sqls.callback.entities());
        sql.setEntity(nutzDao.getEntity(Order.class));
        nutzDao.execute(sql);
        List<Order> list = sql.getList(Order.class);
        return list;
    }

    public void insert(MSNotify notify){
        notify.setId(CodeGenerator.getUUID32());
        nutzDao.insert(notify);
    }
    public void update(MSNotify notify){
        nutzDao.update(notify);
    }
    public MSNotify getNotify(String id){
        return nutzDao.fetch(MSNotify.class,id);
    }
}
